package com.mate.cloud.xml;

import junit.framework.TestCase;
import org.apache.xalan.processor.TransformerFactoryImpl;
import org.apache.xerces.jaxp.SAXParserFactoryImpl;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.dom4j.util.NodeComparator;

import java.io.File;
import java.io.InputStream;

/**
 * An abstract base class for some DOM4J test cases
 *
 * @author: MI
 * @email: 448341911@qq.com
 * @createTime: 2024/6/2 19:43
 * @updateUser: MI
 * @updateTime: 2024/6/2 19:43
 * @updateRemark: 修改内容
 * @version: v1.0
 */
public class AbstractTestCase extends TestCase {

    protected Document document;

    protected AbstractTestCase() {
        super();
    }

    protected AbstractTestCase(String name) {
        super(name);
    }

    protected void log(String text) {
        System.out.println(text);
    }

    protected Document getDocument() {
        return document;
    }

    protected Document getDocument(String path) throws Exception {
        return getDocument(path, new SAXReader());
    }

    // 读取并解析XML文件的方法
    protected Document getDocument(InputStream inputStream, SAXReader reader) throws Exception {
        return reader.read(inputStream);
    }

    protected Document getDocument(String path, SAXReader reader) throws Exception {
        return reader.read(getFile(path));
    }

    protected File getFile(String path) {
        return new File(System.getProperty("user.dir"), path);
    }

    public void assertDocumentsEqual(Document doc1, Document doc2) throws Exception {
        try {
            assertTrue("Doc1 not null", doc1 != null);
            assertTrue("Doc2 not null", doc2 != null);

            doc1.normalize();
            doc2.normalize();

            assertNodesEqual(doc1, doc2);

            NodeComparator comparator = new NodeComparator();
            assertTrue("Documents are equal", comparator.compare(doc1, doc2) == 0);
        } catch (Exception e) {
            log("Failed during comparison of: " + doc1 + " and: " + doc2);
            throw e;
        }
    }

    public void assertNodesEqual(Document n1, Document n2) {
        // assertEquals( "Document names", n1.getName(), n2.getName() );
        assertNodesEqual(n1.getDocType(), n2.getDocType());
        assertNodesEqualContent(n1, n2);
    }

    public void assertNodesEqualContent(Branch b1, Branch b2) {
        int c1 = b1.nodeCount();
        int c2 = b2.nodeCount();

        if (c1 != c2) {
            log("Content of: " + b1);
            log("is: " + b1.content());
            log("Content of: " + b2);
            log("is: " + b2.content());
        }

        assertEquals("Branches have same number of children (" + c1 + ", " + c2 + " for: " + b1 + " and " + b2, c1, c2);

        for (int i = 0; i < c1; i++) {
            Node n1 = b1.node(i);
            Node n2 = b2.node(i);
            assertNodesEqual(n1, n2);
        }
    }

    public void assertNodesEqual(Node n1, Node n2) {
        int nodeType1 = n1.getNodeType();
        int nodeType2 = n2.getNodeType();
        assertTrue("Nodes are of same type: ", nodeType1 == nodeType2);

        switch (nodeType1) {
            case Node.ELEMENT_NODE:
                assertNodesEqual((Element) n1, (Element) n2);

                break;

            case Node.DOCUMENT_NODE:
                assertNodesEqual((Document) n1, (Document) n2);

                break;

            case Node.ATTRIBUTE_NODE:
                assertNodesEqual((Attribute) n1, (Attribute) n2);

                break;

            case Node.TEXT_NODE:
                assertNodesEqual((Text) n1, (Text) n2);

                break;

            case Node.CDATA_SECTION_NODE:
                assertNodesEqual((CDATA) n1, (CDATA) n2);

                break;

            case Node.ENTITY_REFERENCE_NODE:
                assertNodesEqual((Entity) n1, (Entity) n2);

                break;

            case Node.PROCESSING_INSTRUCTION_NODE:
                assertNodesEqual((ProcessingInstruction) n1, (ProcessingInstruction) n2);

                break;

            case Node.COMMENT_NODE:
                assertNodesEqual((Comment) n1, (Comment) n2);

                break;

            case Node.DOCUMENT_TYPE_NODE:
                assertNodesEqual((DocumentType) n1, (DocumentType) n2);

                break;

            case Node.NAMESPACE_NODE:
                assertNodesEqual((Namespace) n1, (Namespace) n2);

                break;

            default:
                assertTrue("Invalid node types. node1: " + n1 + " and node2: " + n2, false);
        }
    }

    // Implementation methods
    // -------------------------------------------------------------------------
    protected void setUp() throws Exception {
        System.setProperty("javax.xml.parsers.SAXParserFactory",
                SAXParserFactoryImpl.class.getName());
        System.setProperty("javax.xml.transform.TransformerFactory",
                TransformerFactoryImpl.class.getName());
        document = DocumentHelper.createDocument();

        Element root = document.addElement("root");

        Element author1 = root.addElement("author").addAttribute("name",
                "James").addAttribute("location", "UK").addText(
                "James Strachan");

        Element url1 = author1.addElement("url");
        url1.addText("http://sourceforge.net/users/jstrachan/");

        Element author2 = root.addElement("author").addAttribute("name", "Bob")
                .addAttribute("location", "Canada").addText("Bob McWhirter");

        Element url2 = author2.addElement("url");
        url2.addText("http://sourceforge.net/users/werken/");
    }


    /**
     * DOCUMENT ME!
     *
     * @return the root element of the document
     */
    protected Element getRootElement() {
        Element root = document.getRootElement();
        assertTrue("Document has root element", root != null);

        return root;
    }
}
